package com.ruoyi.mushcoin.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.core.core.domain.R;
import com.ruoyi.common.excel.core.ExcelResult;
import com.ruoyi.common.excel.utils.ExcelUtil;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.orm.core.page.TableDataInfo;
import com.ruoyi.common.web.annotation.RepeatSubmit;
import com.ruoyi.common.web.core.BaseController;
import com.ruoyi.mushcoin.domain.bo.TgLanguageDataBo;
import com.ruoyi.mushcoin.domain.vo.TgLanguageDataImportVo;
import com.ruoyi.mushcoin.domain.vo.TgLanguageDataVo;
import com.ruoyi.mushcoin.listener.TgLanguageDataImportListener;
import com.ruoyi.mushcoin.service.ITgLanguageDataService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * TG多国语言Controller
 *
 * @author billy
 * 2025-03-05
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/mushcoin/languageData")
public class TgLanguageDataController extends BaseController {
    @Resource
    private ITgLanguageDataService tgLanguageDataService;

    /**
     * 查询TG多国语言列表
     */
    @SaCheckPermission("mushcoin:languageData:list")
    @GetMapping("/list")
    public TableDataInfo<TgLanguageDataVo> list(TgLanguageDataBo tgLanguageDataBo) {
        return tgLanguageDataService.selectPage(tgLanguageDataBo);
    }

    /**
     * 导出TG多国语言列表
     */
    @SaCheckPermission("mushcoin:languageData:export")
    @Log(title = "TG多国语言", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TgLanguageDataBo tgLanguageDataBo) {
        List<TgLanguageDataVo> list = tgLanguageDataService.selectList(tgLanguageDataBo);
        ExcelUtil.exportExcel(list, "TG多国语言", TgLanguageDataVo.class, response);
    }

    /**
     * 导入数据
     *
     * @param file          导入文件
     * @param updateSupport 是否更新已存在数据
     */
    @Log(title = "TG多国语言", businessType = BusinessType.IMPORT)
    @SaCheckPermission("mushcoin:languageData:import")
    @PostMapping("/importData")
    public R<Void> importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelResult<TgLanguageDataImportVo> result = ExcelUtil.importExcel(file.getInputStream(), TgLanguageDataImportVo.class, new TgLanguageDataImportListener(updateSupport));
        return R.ok(result.getAnalysis());
    }

    @SaCheckPermission("mushcoin:languageData:import")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "TG多国语言", TgLanguageDataImportVo.class, response);
    }

    /**
     * 获取TG多国语言详细信息
     */
    @SaCheckPermission("mushcoin:languageData:query")
    @GetMapping(value = "/{id}")
    public R<TgLanguageDataVo> getInfo(@PathVariable Long id) {
        return R.ok(tgLanguageDataService.selectById(id));
    }

    /**
     * 新增TG多国语言
     */
    @SaCheckPermission("mushcoin:languageData:add")
    @Log(title = "TG多国语言", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping
    public R<Void> add(@Validated @RequestBody TgLanguageDataBo tgLanguageDataBo) {
        boolean inserted = tgLanguageDataService.insert(tgLanguageDataBo);
        if (!inserted) {
            return R.fail("新增TG多国语言记录失败！");
        }
        return R.ok();
    }

    /**
     * 修改TG多国语言
     */
    @SaCheckPermission("mushcoin:languageData:edit")
    @Log(title = "TG多国语言", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping
    public R<Void> edit(@Validated @RequestBody TgLanguageDataBo tgLanguageDataBo) {
        Boolean updated = tgLanguageDataService.update(tgLanguageDataBo);
        if (!updated) {
            return R.fail("修改TG多国语言记录失败!");
        }
        return R.ok();
    }

    /**
     * 删除TG多国语言
     */
    @SaCheckPermission("mushcoin:languageData:remove")
    @Log(title = "TG多国语言", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@PathVariable Long[] ids) {
        boolean deleted = tgLanguageDataService.deleteByIds(ids);
        if (!deleted) {
            return R.fail("删除TG多国语言记录失败!");
        }
        return R.ok();
    }
}
